<template>
  <view class="car-warp">
    <!-- #ifdef H5 -->
    <van-nav-bar @click-left="onClickLeft" left-arrow title="添加车辆"></van-nav-bar>
    <!-- #endif -->
    <!-- 信息编辑 -->
    <view class="content">
      <view class="item">
        <view class="itemname">
          <text class="red">*</text>
          车牌号
        </view>
        <view class="itemname-detail">
          <input
            class="user-input"
            type="text"
            v-model="form.plateNumber"
            maxlength="30"
            placeholder="请输入车牌号"
            placeholder-style="color:#545454;font-size:14px" />
        </view>
      </view>
      <view class="line"></view>
      <view class="item">
        <view class="itemname">所有人</view>
        <view class="itemname-detail">
          <input
            class="user-input"
            type="text"
            v-model="form.owner"
            maxlength="30"
            placeholder="请输入所有人"
            placeholder-style="color:#545454;font-size:14px" />
        </view>
      </view>
      <view class="line"></view>
      <view class="item">
        <view class="itemname">车辆类型</view>
        <view class="itemname-detail">
          <input
            class="user-input"
            type="text"
            v-model="form.type"
            maxlength="30"
            placeholder="请输入车辆类型"
            placeholder-style="color:#545454;font-size:14px" />
        </view>
      </view>
      <view class="line"></view>
      <view class="item">
        <view class="itemname">品牌型号</view>
        <view class="itemname-detail">
          <input
            class="user-input"
            type="text"
            v-model="form.model"
            maxlength="30"
            placeholder="示例：丰田牌GIM7250"
            placeholder-style="color:#545454;font-size:14px" />
        </view>
      </view>
      <view class="line"></view>
      <view class="item">
        <view class="itemname">
          <text class="red">*</text>
          车辆识别代码
        </view>
        <view class="itemname-detail">
          <input
            class="user-input"
            type="text"
            v-model="form.vin"
            maxlength="30"
            placeholder="请输入车辆识别代码"
            placeholder-style="color:#545454;font-size:14px" />
        </view>
      </view>
      <view class="line"></view>
    </view>
    <view class="bottom-wrap" :style="styleBntH">
      <button class="bnt-wrap bnt" @tap="submit">保存</button>
    </view>
  </view>
</template>

<script lang="ts" setup>
import { reactive } from 'vue';
import { getSystemInfo, getUserId, setToastInstance, systemInfo } from '@mixins/info';
import { addCar, checkCarRepeat } from '@/service/apis/mine';
import { getItem } from '@/utils/util';
// #ifdef MP-WEIXIN
import { useToast } from '@/components/wxToast';
setToastInstance(useToast());
// #endif

// 分享
onShareAppMessage(() => {
  return {
    title: '',
    path: '/pages/stationMap/stationMap',
  };
});

const $toast: any = inject('$Toast');

const styleBntH = `height: calc(124rpx + ${getSystemInfo().safeBottom}px)`;

const form = reactive({
  plateNumber: '',
  owner: '',
  type: '',
  model: '',
  vin: '',
  isDefault: true,
});

const submit = () => {
  if (!form.plateNumber) {
    $toast('请输入车牌号');
    return;
  }
  if (!form.vin) {
    $toast('请输入车辆识别代码');
    return;
  }
  checkCarRepeat(form, { account: { accountId: getUserId() } }).then((res: any) => {
    if (res) {
      addCar(form, { account: { accountId: getItem('userid') } }).then((res: any) => {
        console.log(res);
        if (res) {
          uni.navigateBack({
            delta: 1,
          });
        }
      });
    } else {
      uni.showModal({
        content: `当前车辆已被其他用户绑定，您的操作会将前一个用户的车辆解绑，是否确认您的信息无误并提交？`,
        showCancel: true,
        success: res => {
          if (res.confirm) {
            addCar(form, { account: { accountId: getItem('userid') } }).then((res: any) => {
              console.log(res);
              if (res) {
                uni.navigateBack({
                  delta: 1,
                });
              }
            });
          } else {
            console.log('cancel'); //点击取消之后执行的代码
          }
        },
      });
    }
  });
};
// #ifdef H5
const onClickLeft = () => {
  uni.navigateBack();
};
setTimeout(() => {
  const navBarEle = document.querySelector('.van-nav-bar') as HTMLElement;
  navBarEle.style.paddingTop = `${systemInfo.statusBarHeight}px`;
});
// #endif
</script>

<style lang="scss" scoped>
.car-warp {
  .content {
    padding: 0 16px;
    .title {
      font-size: 14px;
      line-height: 22px;
      color: #858585;
      margin: 12px 0 8px 0;
    }
    .item {
      background-color: #ffffff;
      display: flex;
      justify-content: space-between;
      margin: 0 auto;
      height: 56px;
      line-height: 56px;
      align-items: center;
      .itemname {
        font-size: 16px;
        color: #242424;
        white-space: nowrap;
      }
      .itemname-detail {
        margin-left: 15px;
        // width: calc(100% - 108px);
        color: #545454;
        text-align: right;
      }
      .red {
        color: $uni-color-error;
      }
    }
  }

  .user-input {
    // width: 60vw;
    height: 15vw;
    font-size: 32rpx;
    color: #000000;
  }

  .line {
    border-bottom: 1px solid #f2f2f2;
    height: 1px;
    position: relative;
  }
  .bottom-wrap {
    display: flex;
    position: fixed;
    width: 100%;
    bottom: 0;
    background: #fff;
    border-top: 1px solid #f2f2f2;
    box-sizing: border-box;
    .bnt {
      height: 44px;
      width: 343px;
      margin-top: 9px;
      border-radius: 50px;
    }
  }
}
</style>
